<template>
  <div>
    <el-table :data="postList" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" justify="center"></el-table-column>
      <el-table-column label="缩略图" width="180">
        <template slot-scope="scope">
          <img
            v-if="scope.row.cover.length>0"
            :src="fixImgUrl(scope.row.cover[0].url)"
            alt
            class="thumbnail"
          />
          <img v-else src="../assets/timg (1).jpg" alt class="thumbnail" />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="文章标题" width="350"></el-table-column>
      <el-table-column prop="user.nickname" label="作者" width="100"></el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">{{scope.row.create_date.split("T")[0]}}</template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">{{scope.row.create_date.split("T")[0]}}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="$router.push('/editPost?id='+scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!-- <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="currentChange"
      @size-change="sizeChange"
    ></el-pagination>-->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page="currentPage"
      :page-sizes="[3,4,5]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      postList: [],
      total: 0,
      pageIndex: 1,
      pageSize: 3,
      currentPage: 1,
    };
  },
  created() {
    this.loadPost();
  },
  methods: {
    loadPost() {
      // 发起请求
      this.$axios({
        url: "/post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        console.log(res.data);
        this.postList = res.data.data;
        this.total = res.data.total;
      });
    },
    // 当前页
    currentChange(newIndex) {
      this.pageIndex = newIndex;
      //   console.log("123");
      this.loadPost();
    },
    // 每页条数
    sizeChange(newSize) {
      this.pageSize = newSize;
      this.loadPost();
    },
    // 判断图片路径是否完整
    fixImgUrl(oldUrl) {
      if (oldUrl.indexOf("http") > -1) {
        return oldUrl;
      } else {
        return this.$axios.defaults.baseURL + oldUrl;
      }
    },
    del() {
      alert("删除成功");
    },
  },
};
</script>

<style lang="less" scoped>
.thumbnail {
  width: 50px;
  height: 50px;
  object-fit: cover;
}
</style>